package main
import (
	"fmt"
	"math"
)

type geometry interface{
	area() float64
	perim() float64
}

type rect struct{
	width,height float64
}

type circle struct{
	radius float64
}

func (r rect) area() float64{
	return r.width * r.height
}
func (r rect) perim() float64{
	return 2*r.width + 2*r.height
}
func (c circle) area() float64{
	return math.Pi * c.radius * c.radius
}
func (c circle) perim() float64 {
    return 2 * math.Pi * c.radius
}

func measure(g geometry){
	fmt.Println(g)
	fmt.Println(g.area())
	fmt.Println(g.perim())
}


// 语法：
// 		- 类型断言：g.(circle) 是类型断言（type assertion）语法，意思是“尝试把 g 转换为 circle 类型”
// 		- if：ok 是一个布尔值，表示断言是否成功
func detectCircle(g geometry){
	if  c,ok := g.(circle); ok {
		fmt.Println("Circle with radius:", c.radius)
	}
}

func main(){
	r := rect{20,30}
	c := circle{5}

	measure(r)
	measure(c)

		
    detectCircle(r)
    detectCircle(c)

}
